package com.miyu.system.service.user.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.utils.commonUtils.Comutils;
import com.miyu.system.dao.system.SysUserDao;
import com.miyu.system.pojo.system.SysUser;
import com.miyu.system.service.user.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 系统用户服务impl
 *
 * @author longm
 * @date 2022/10/03
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao dao;
    /**
     * 保存系统用户
     *
     * @param sysUser 系统用户
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject saveSysUser(JSONObject sysUser) {
        JSONObject rs = new JSONObject();
        String userId = Comutils.getUUID();
       SysUser user =  JSONObject.parseObject(sysUser.toString(),SysUser.class);
        if(StringUtils.isBlank(user.getUserId())){
            user.setUserId(userId);
        }
        dao.save(user);
        rs.put("code","1");
        rs.put("msg","操作成功");
        return rs;
    }

    /**
     * 删除系统用户
     *
     * @param params 参数个数
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject deleteSysUser(JSONObject params) {
        JSONObject rs = new JSONObject();
        SysUser user =  JSONObject.parseObject(params.toString(),SysUser.class);
        if(StringUtils.isNotBlank(user.getUserId())){
            dao.deleteById(user.getUserId());
            rs.put("code","1");
            rs.put("msg","操作成功");
        }else {
            rs.put("code","-1");
            rs.put("msg","操作失败");
        }
        return rs;
    }


    /**
     * 查询系统用户
     *
     * @param params 参数个数
     * @return {@link JSONArray}
     */
    @Override
    public JSONArray querySysUser(JSONObject params) {
        JSONArray rs = new JSONArray();
        return rs = dao.querySysUser(params.getString("info"));
    }
}
